package com.taotao.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.taotao.web.bean.ItemVO;
import com.taotao.web.bean.Order;
import com.taotao.web.bean.User;
import com.taotao.web.handlerInterceptor.UserLoginHandlerInterceptor;
import com.taotao.web.service.ItemService;
import com.taotao.web.service.OrderService;
import com.taotao.web.service.UserService;
import com.taotao.web.threadlocal.UserThreadLocal;

@Controller
@RequestMapping("order")
public class OrderController {
	@Autowired
	private ItemService itemService;
	@Autowired
	private UserService userService;
	@Autowired
	private OrderService orderService;
	
@RequestMapping(value="{itemId}",method=RequestMethod.GET)
	public ModelAndView toOrder(@PathVariable(value="itemId")Long itemId){
		ModelAndView mv = new ModelAndView("order");
		ItemVO item = this.itemService.queryById(itemId);
		mv.addObject("item",item);
		return mv ;
		
	}
/**
 * 提交订单
 * @param order
 * @param token
 * @return
 */
@RequestMapping(value="submit",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> submit(Order order){
	//查询用户信息,从线程中
	User user = UserThreadLocal.get();
	//添加数据
	order.setUserId(user.getId());
	order.setBuyerNick(user.getUsername());
			Map<String, Object> result = new HashMap<String, Object>();
			String orderId = this.orderService.submit(order);
			if (StringUtils.isEmpty(orderId)) {
				//为空订单提交失败
				result.put("status", 300);
			}else{
				//为空订单提交成功
				result.put("status", 200);
				result.put("data", orderId);
			}
			return result ;
	
}


}
